package com.uduew.sjd.utils

import android.content.Context
import android.net.Uri
import android.provider.ContactsContract

object FormUtils {

    data class ContactInfo(val name: String, val phone: String)

    fun getContactInfo(context: Context,uri: Uri): ContactInfo {
        var phoneNumber = ""
        var contactName = ""
        try {
            context.contentResolver?.query(
                uri,
                arrayOf(
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME // 添加姓名字段
                ),
                null,
                null,
                null
            )?.use { cursor ->
                if (cursor.moveToFirst()) {
                    // 获取电话号码
                    val phoneIndex =
                        cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                    phoneNumber = cursor.getString(phoneIndex)
                    // 获取联系人姓名
                    val nameIndex =
                        cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                    contactName = cursor.getString(nameIndex)
                    contactName= contactName.replace(" ", "")
                    // 清理电话号码格式
                    phoneNumber = phoneNumber.replace(" ", "")
//                        .replace("-", "")
//                        .replace("+84", "")
                }
            }
        } catch (e: Exception) {
        }
        return ContactInfo(contactName, phoneNumber)
    }
}